/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhouyou.http.demo.view;

import com.zhouyou.http.demo.ResourceTable;
import com.zhouyou.http.utils.HttpLog;
import com.zhouyou.http.view.CancelableDialog;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.text.NumberFormat;

public class HorizontalProgressDialog extends CancelableDialog {
    private int max = 0;
    private EventHandler mViewUpdateHandler;
    private Text mProgressNumber;
    private Text mProgressPercent;
    private Text mTitleView;
    private Text mMessgeView;
    private ProgressBar mProgress;
    private int mMax;
    private boolean mHasStarted = true;
    private int mProgressVal;
    private CharSequence mMessage;
    private String mProgressNumberFormat;
    private NumberFormat mProgressPercentFormat;

    public Text getMessgeView() {
        return mMessgeView;
    }
   public  Text getTitleView(){
        return  mTitleView;
   }
   public  ProgressBar getProgress(){
        return  mProgress;
   }
    private void initFormats() {
        mProgressNumberFormat = "%1d/%2d";
        mProgressPercentFormat = NumberFormat.getPercentInstance();
        mProgressPercentFormat.setMaximumFractionDigits(0);
    }


    public HorizontalProgressDialog(Context context) {
        super(context);
        Component component = LayoutScatter.getInstance(this.context).parse(ResourceTable.Layout_horizontal_progress_dialog, null, true);
        mProgress = (ProgressBar) component.findComponentById(ResourceTable.Id_progress);
        mProgressNumber = (Text) component.findComponentById(ResourceTable.Id_progress_number);
        mProgressPercent = (Text) component.findComponentById(ResourceTable.Id_progress_percent);
        mTitleView = (Text) component.findComponentById(ResourceTable.Id_title);
        mMessgeView = (Text) component.findComponentById(ResourceTable.Id_messge);

        initFormats();
        mViewUpdateHandler = new UIHandler(EventRunner.getMainEventRunner(), mProgress, mProgressNumber, mProgressPercent, mProgressNumberFormat, mProgressPercentFormat);
        setContentCustomComponent(component);
        if (mMax > 0) {
            setMax(mMax);
        }
        if (mProgressVal > 0) {
            setProgress(mProgressVal);
        }

        if (mMessage != null) {
            setMessage(mMessage);
        }
        onProgressChanged();
    }

    @Override
    protected void onShow() {
        removeDialog();
        super.onShow();
    }

    private void removeDialog() {
        Component component = this.getContentCustomComponent();
        ComponentParent componentParent = component.getComponentParent();
        if (null != componentParent) {
            componentParent.removeComponent(component);
        }
    }

    public void setMessage(String messge) {
        mMessgeView.setText(messge);
    }

    public void setTitle(String title) {
        mTitleView.setText(title);
    }

    public void setProgress(int value) {
        if (mHasStarted) {
            mProgress.setProgressValue(value);
            onProgressChanged();
        } else {
            mProgressVal = value;
        }
    }


    public void setMax(int max) {
        if (mProgress != null) {
            onProgressChanged();
        } else {
            mMax = max;
        }
    }

    public void setMessage(CharSequence message) {
        if (mProgress != null) {

            mMessage = message;

        }
    }

    private void onProgressChanged() {
        if (mViewUpdateHandler != null && !mViewUpdateHandler.hasInnerEvent(0)) {
            mViewUpdateHandler.sendEvent(0);
        }
    }


    private static class UIHandler extends EventHandler {
        private Text mProgressNumber;
        private Text mProgressPercent;
        private ProgressBar mProgress;
        private String mProgressNumberFormat;
        private NumberFormat mProgressPercentFormat;

        public UIHandler(EventRunner runner, ProgressBar progress, Text progressNumber, Text progressPercent, String progressNumberFormat, NumberFormat progressPercentFormat) throws IllegalArgumentException {
            super(runner);
            this.mProgress = progress;
            this.mProgressNumber = progressNumber;
            this.mProgressPercent = progressPercent;
            this.mProgressNumberFormat = progressNumberFormat;
            this.mProgressPercentFormat = progressPercentFormat;
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event == null) {
                return;
            }
            int eventId = event.eventId;
            long param = event.param;
            switch (eventId) {
                case 0:
                    // 待执行的操作，由开发者定义
                    /* Update the number and percent */
                    int progress = mProgress.getProgress();
                    int max = mProgress.getMax();
                    if (mProgressNumberFormat != null) {
                        String format = mProgressNumberFormat;
                        mProgressNumber.setText(String.format(format, progress, max));
                    } else {
                        mProgressNumber.setText("");
                    }
                    if (mProgressPercentFormat != null) {
                        double percent = (double) progress / (double) max;
                        mProgressPercent.setText(mProgressPercentFormat.format(percent));
                    } else {
                        mProgressPercent.setText("");
                    }
                    break;
                default:
                    break;
            }


        }
    }
}
